@php
    $user = Auth::user();
    $role = $user->roles()->get();
	$role_id = $user->cur_role_id;
@endphp
@extends('layouts.default')

@section('title', '')

@push('scripts')
<script src="{{ asset('lib/xm-select-v1.2.4/xm-select.js')}}"></script>
<script>
    layui.use(['layer', 'element','form','table','laypage','laytpl','laydate'], function(){
        var layer = layui.layer;
        var form = layui.form;
        var element = layui.element;
        var table = layui.table;
        var laypage = layui.laypage;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var param = {}; 
        var xmDemo3; //调取资料时间

        laydate.render({
            elem: '#check_start'
        });
        laydate.render({
            elem: '#check_end'
        });
    
     
        getList(1);
        renderSelect();

        //日期时间范围
        laydate.render({
            elem: '#date'
            ,type: 'date'
            ,range: true
        });
      
        //下拉框初始化
        function renderSelect(){
            getDictionaryItem({'diccode': ['award_type','LEVEL','award_grade']}, function (res){
                if(res.code === 1) {  
                    let data = res.data;
                    for(var k in data) {
                        if(k == 'hdjb'){   
                            $('[data-role='+k+'List]').empty().append('<option value="">请选择</option>');
                        }else{
                            $('[data-role='+k+'List]').empty().append('<option value="">请选择</option>');
                        }
                        let d = data[k];
                        for (var i in d) {
                            $('[data-role='+k+'List]').append('<option value="' + d[i].optionId + '">' + d[i].selectValue + '</option>');
                        }
                    }
                    form.render(); 
                }
            });  
            getTermData({},function(res){
                if(res.code == 1){
                    $('select[name=data_version_id]').empty().append('<option value="">请选择</option>');
                    $('select[name=data_version_id2]').empty().append('<option value="">请选择</option>');
                    for(var i in res.data){
                        $('select[name=data_version_id]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                        $('select[name=data_version_id2]').append('<option value="'+res.data[i].id+'">'+res.data[i].title+'</option>');
                    }
                    form.render(); 
                    //调取资料时间
                    xmDemo3 = xmSelect.render({
                        el: '#demo3',                            
                        autoRow: true,
                        theme: {
                            color: '#2d8cf0',
                        },
                        prop: {
                            name: 'title',
                            value: 'id',
                        },
                        name:'materials_date',
                        layVerify: 'required',
                        layVerType: 'msg',
                        layReqText:'调取资料时间不能为空',
                        data: res.data
                    })
                }
            })   
        }

        // 查询
        form.on('submit(queryBtn)', function(data) {
            Object.assign(param, {
                    "search": data.field
            });
            getList(1);
            return false; 
        });
       
        //重置
        $('button[type=reset]').click(function(){
            $('#search-form')[0].reset();
            $('button[lay-filter=queryBtn]').click(); 
            return false;    
        })

        //汇总表导出
        $('#hzbdc').click(function(){
            let data_version_id = $('select[name=data_version_id]').val();
            let data_version_name = $('select[name=data_version_id]').find("option:selected").text();
            if(data_version_id == ''){
                layer.msg('请选择学期');return false;
            }
            Object.assign(param,{"data_version_name":data_version_name});	
            finalExport(param,function(res){
                if(res.code == '1'){
                    exportsDel(res.data.file_name,'/download/zip/'+res.data.file_name);
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            })
            return false;
        })

        //导出excel
        function exportsDel(showName,fileName){
            window.location.href = "{{route('download.FileDownloadDel')}}"+'?filename='+showName+'&file_url='+fileName;
        }

        function getList(page){
            Object.assign(param,{"page":page});		        		        	
            getCheckList(param,function(res){		
                if(res.code == 1){
                    table.render({
                        elem: '#tablelist'
                        ,toolbar: '#toolbarDemo'
                        ,defaultToolbar:[]
                        ,data: res.data.data
                        ,limit:res.data.limit
                        ,skin:'nob'
                        ,even:true
                        //,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                        ,cols: [[ 
                        {field: '', title: '学年学期',width:'20%',align:'center',templet: function(d) {
                            // return (page-1)*res.data.limit+d.LAY_INDEX;
                            if(d.data_version_name === null){
                                return '';
                            }else{
                                return d.data_version_name;
                            }                        
                        }}							                  							
                        ,{field: 'name', title: '名称',width:'20%',align:'center'}                     
                        ,{field: '', title: '考核起止时间',width:'20%',align:'center',templet: function(d) {
                            if(d.check_start !== null && d.check_end !== null){
                                return d.check_start+' - '+d.check_end; 
                            }else{
                                return '';
                            }                                                 
                        }}
                        ,{field: 'submit_text', title: '状态',width:'10%',align:'center',templet: function(d) {
                            return d.submit_text;                                                
                        }}	                                            													
                        ,{field: '', title: '操作',toolbar: '#barDemo',width:'',align:'center'}
                        ]]
                    });

                    $('#pageBar').empty();                     
                    if(res.data.total >0){
                        //分页
                        laypage.render({
                        elem: 'pageBar' //注意，这里的 test1 是 ID，不用加 # 号
                        ,count: [res.data.total] //数据总数，从服务端得到
                        ,curr: page
                        ,layout: ['limit', 'count', 'prev', 'page', 'next',  'skip']                               
                        ,limit:res.data.limit
                        ,jump: function(obj, first) {
                                //首次不执行
                                if (!first) {
                                    Object.assign(param,{"limit":obj.limit});                                
                                    getList(obj.curr)
                                }
                            }
                        });
                    } 
                     setBtnPermission();                           
                }else {
                    layer.msg(res.msg, {icon: 5});
                } 
                                    
            })
        }



        table.on('tool(tablelist)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
     
            if(layEvent === 'del'){ //删除
                layer.confirm('确定删除吗?', function(index){
                    delCheck({'id':data.id},function(res){
                        if(res.code == '1'){
                            layer.msg(res.msg,{icon: 6,time: 1000});
                            obj.del(); 
                            layer.close(index);
                        }else{
                            layer.msg(res.msg, {icon: 5,shift : 6});
                        }
                    })                             
                });
            }else if(layEvent === 'edit'){     
              window.location.href = "{{route('rating.check.add')}}?id="+data.id;    
            }else if(layEvent === 'checkSee'){
                window.location.href = "{{route('rating.check.checkSee')}}?id="+data.id+'&submit_text='+data.submit_text;
            }else if(layEvent === 'export'){
                checkExport({'id':data.id},function(res){
                    if(res.code == '1'){
                        exportsDel(res.data.file_name,'/download/zip/'+res.data.file_name);
                    }else{
                        layer.msg(res.msg, {icon: 5,shift : 6});
                    }
                })
            }else if(layEvent === 'fuyong'){
                layer.open({
                    title:'复用当前数据',
                    type: 1,
                    area: ['800px', '650px'], // 宽高
                    content: $('#view-fuyong'),
                    btn:['确认','取消'],
                    btnAlign:'c',
                    btn1: function(index, layero, that){                            
                        $('#add-fuyong').click();
                        var data = form.val('formTest');
                        data.materials_date = xmDemo3.getValue('value');
                        data.materials_text = xmDemo3.getValue('nameStr');
                      
                        copyCheck(data,function(res){
                            if(res.code == '1'){
                                layer.closeAll();
                                layer.msg(res.msg,{icon: 6,time: 1000},function(){
                                    getList(1);
                                });                     
                            }else{
                                layer.msg(res.msg, {icon: 5,shift : 6});
                            }
                        })
                        return false;
                    },
                    btn2: function(index, layero, that){
                        $('#view-fuyong').hide();
                    },
                    cancel: function(index, layero, that){
                        $('#view-fuyong').hide();
                    },
                    success: function(layero, index, that){
                        form.val("formTest", {                     
                            'data_version_id2': data.data_version_id,
                            'name': data.name,
                            'check_start': data.check_start,
                            'check_end': data.check_end,
                            'id': data.id,                        
                        }); 
                        //调取资料时间
                        xmDemo3.setValue(data.materials_array);
                    }
                });
            }else if(layEvent === 'caozuo'){         
                layer.open({
                    title:'审核小组操作',
                    type: 1,
                    area: ['800px', '750px'], // 宽高
                    content: $('#view-caozuo'),
                    btn:['取消'],
                    btnAlign:'c',
                    btn1: function(index, layero, that){ 
                        layer.closeAll();                           
                        $('#view-caozuo').hide();
                        return false;
                    },
                    cancel: function(index, layero, that){
                        $('#view-caozuo').hide();
                    },
                    success: function(layero, index, that){
                        $('input[name=check_id]').val(data.id);
                        table.render({
                            elem: '#ID-table-demo-data1'
                            ,data: data.leader !== null ?data.leader_array.all:[]
                            ,skin:'nob'
                            ,cols: [[ 						                  							
                            {field: 'name', title: '（审核组长）姓名',width:'50%',align:'center'} 	                                            													
                            ,{field: '', title: '操作',toolbar: '#shanchu1',width:'50%',align:'center'}
                            ]]
                        });
                        table.render({
                            elem: '#ID-table-demo-data2'
                            ,data: data.members !== null?data.members_array.all:[]
                            ,skin:'nob'
                            ,cols: [[ 						                  							
                            {field: 'name', title: '（审核成员）姓名',width:'50%',align:'center'} 	                                            													
                            ,{field: '', title: '操作',toolbar: '#shanchu2',width:'50%',align:'center'}
                            ]]
                        });
                    }
                });
            }
        });

        //成员删除
        $(document).on('click','.cusc1',function(){
            let id = $('input[name=check_id]').val();
            let value = $(this).attr('value');
            layer.confirm('删掉当前审核成员，历史打分数据将不会保留，并重新计算分数，确定吗?', function(index){
                layer.close(index);
                checkMemberDel({id:id,value:value,type:'leader'},function(res){               
                        if(res.code == '1'){
                            layer.msg(res.msg,{icon: 6,time: 1000},function(){
                                table.reload('ID-table-demo-data1', {
                                    data:res.data.leader===null?[]:res.data.leader
                                });
                            });                     
                        }else{
                            layer.msg(res.msg, {icon: 5,shift : 6});
                        }                   
                })                         
            });         
        })
        $(document).on('click','.cusc2',function(){
            let id = $('input[name=check_id]').val();
            let value = $(this).attr('value');
            layer.confirm('删掉当前审核成员，历史打分数据将不会保留，并重新计算分数，确定吗?', function(index){
                layer.close(index);
                checkMemberDel({id:id,value:value,type:'members'},function(res){
                    if(res.code==1){
                        layer.msg(res.msg,{icon: 6,time: 1000},function(){
                            table.reload('ID-table-demo-data2', {
                                data:res.data.members===null?[]:res.data.members
                            });
                        }); 
                    }else{
                        layer.msg(res.msg, {icon: 5,shift : 6});
                    } 
                })                         
            });
        })

       
        //远程搜索添加审核人员
        var leader = xmSelect.render({
            el: '#leader', 
            filterable: true,
            remoteSearch: true,
            searchTips: '姓名搜索',
            radio: true,
            theme: {
                color: '#2d8cf0',
            },
            template({ item, sels, name, value }){
                return item.name + '<span style="position: absolute; right: 10px; color: #8799a3">'+item.username+'</span>' 
            },
            remoteMethod: function(val, cb, show){
                if(!val){
                    return cb([]);
                }
                getcheckMember({name:val},function(res){
                    if(res.code==1){
                        if(res.data.length>0){
                            return cb(res.data);
                        }else{
                            return cb([]);
                        }
                    }else{
                        return cb([]);
                    }
                })
            },
            data: []
        })
        var members = xmSelect.render({
            el: '#members', 
            filterable: true,
            remoteSearch: true,
            searchTips: '姓名搜索',
            radio: true,
            theme: {
                color: '#2d8cf0',
            },
            template({ item, sels, name, value }){
                return item.name + '<span style="position: absolute; right: 10px; color: #8799a3">'+item.username+'</span>' 
            },
            remoteMethod: function(val, cb, show){
                if(!val){
                    return cb([]);
                }               
                getcheckMember({name:val},function(res){
                    if(res.code==1){
                        if(res.data.length>0){
                            return cb(res.data);
                        }else{
                            return cb([]);
                        }
                    }else{
                        return cb([]);
                    }
                })
            },
            data: []
        })

        $('.add-qd1').click(function(){
            let value = leader.getValue();
            if(value.length==0){
                layer.msg('不能为空');return false;
            }
            let id = $('input[name=check_id]').val();
            addCheckMember({id:id,value:value,type:'leader'},function(res){
                if(res.code==1){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        table.reload('ID-table-demo-data1', {
                            data:res.data.leader===null?[]:res.data.leader
                        });
                        leader.setValue([ ]);
                    });                   
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            })
        })

        $('.add-qd2').click(function(){
            let value = members.getValue();
            if(value.length==0){
                layer.msg('不能为空');return false;
            }
            let id = $('input[name=check_id]').val();
            addCheckMember({id:id,value:value,type:'members'},function(res){
                if(res.code==1){
                    layer.msg(res.msg,{icon: 6,time: 1000},function(){
                        table.reload('ID-table-demo-data2', {
                            data:res.data.members===null?[]:res.data.members
                        });
                        members.setValue([ ]);
                    });                   
                }else{
                    layer.msg(res.msg, {icon: 5,shift : 6});
                }
            })
        })

        form.on('submit(formDemo)', function(data){
        return false; // 阻止默认 form 跳转
        });
           
        //下载
        function exportsDel(showName,fileName){
            window.location.href = "{{route('download.FileDownloadDel')}}"+'?filename='+showName+'&file_url='+fileName;
        }

    })
</script>
<script type="text/html" id="shanchu1">
    <a class="layui-btn layui-btn-primary layui-btn-xs cusc1" value="@{{d.value}}" >删除</a>
</script>
<script type="text/html" id="shanchu2">
    <a class="layui-btn layui-btn-primary layui-btn-xs cusc2" value="@{{d.value}}" >删除</a>
</script>

<script type="text/html" id="barDemo">
    @{{# if( (d.submit_text == '待提交' || d.submit_text == '未开始') ){ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"  btnPermission="327">编辑</a>
    @{{# }else{ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="327">编辑</a>
    @{{# } }}

    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="fuyong"  btnPermission="353">复用</a>
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="caozuo"  btnPermission="356">操作</a>

    @{{# if( (d.submit_text == '进行中' || d.submit_text == '已结束') && d.is_check == 1 ){ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="checkSee"  btnPermission="326">审核</a> 
    @{{# }else{ }}
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="326">审核</a>
    @{{# } }}

    @{{# if(d.submit_text == '进行中' || d.submit_text == '已结束'){ }}  
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="export"  btnPermission="329">下载</a>
    @{{# }else{ }}   
    <a class="layui-btn layui-btn layui-btn-primary layui-border-blue layui-btn-xs layui-btn-disabled" lay-event=""  btnPermission="329">下载</a>
    @{{# } }} 

    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del" btnPermission="200">删除</a>
</script>
<!---头部工具栏--->
<script type="text/html" id="toolbarDemo">
    <div class="list-head">
            <h3>学年考核</h3>
            <div class="layui-btn-container list-opt-btns">
                <a btnPermission="325" class="layui-btn layui-btn-normal" href="{{route('rating.check.add')}}" id="addBtn"> + 新增 </a>
            </div>
        </div>
</script>

@endpush

@section('content')
    <style>
         /* .layui-table-body{
            overflow: hidden;
        }  */     
    </style>
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row">    
                <form class="layui-form" id="search-form" >   
                    <div class="layui-form-item">             
                        <div class="layui-inline">
                            <label class="layui-form-label" >学年学期:</label>
                            <div class="layui-input-inline">                  
                                <select name="data_version_id" class="layui-select" lay-filter="term_id" data-role="term_id" style="width:190px;"></select>                 
                            </div>
                        </div>
              
                        <div class="layui-inline">
                            <label class="layui-form-label" >名称:</label>
                            <div class="layui-input-inline">                  
                                <input autocomplete="off" type="text" name="name" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>
                  
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 90px;" >考核起止时间:</label>
                            <div class="layui-input-inline">                  
                                <input id="date"  autocomplete="off" type="text" name="date" class="layui-input" placeholder="请输入">                
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label" >状态:</label>
                            <div class="layui-input-inline">                  
                                <select data-role="" name="status"   lay-filter="" >
                                    <option value="">请选择</option>
                                    <option value="0">待提交</option>
                                    <option value="1">未开始</option>
                                    <option value="2">进行中</option>
                                    <option value="3">已结束</option>
                                </select>                 
                            </div>
                        </div>

                        <div class="layui-inline" > 
                                <button lay-submit id="queryBtn" class="pear-btn pear-btn-md pear-btn-primary" lay-filter="queryBtn" >
                                    查询
                                </button>
                                <button type="reset" class="pear-btn pear-btn-md reset">
                                    重置
                                </button>
                                <button  id="hzbdc" class="pear-btn pear-btn-md pear-btn-primary" btnPermission="330">
                                    汇总表导出
                                </button>
                        </div>
                    </div>

                 
                </form>      
            </div>
        </div>
    </div>


    <div class="layui-card">

        <div class="layui-card-body">
            <table class="layui-table" id="tablelist" lay-filter="tablelist" ></table>
            <div class="pagebarbox" style="text-align: end"><div id="pageBar" class="pagebar"></div></div>
        </div>
    </div>
  
   <div id="view-fuyong" style="display:none">

        <p style="margin: 20px 0 0 50px;color:red;" >注：请确认并修改当前复用数据的基础数据（数据处理较慢，5-10分钟后可手动刷新页面查看是否成功）</p>

        <form class="layui-form" action="" style="padding: 30px;" lay-filter="formTest" >
            <input type="hidden" name="id" >
            <div class="layui-form-item">
                <label class="layui-form-label">学年学期：</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                <select class="data_version_id" name="data_version_id2" lay-filter="termList" data-role="termList" lay-verify="required" lay-reqText="学年学期不能为空"></select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">名称：</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入" lay-reqText="名称不能为空" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:100px;">考核起止时间：</label>
                    <div class="layui-inline" id="ID-laydate-range">
                    <div class="layui-input-inline">
                    <input type="text" name="check_start" id="check_start" required lay-verify="required" placeholder="请输入" lay-reqText="考核开始不能为空" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline">
                    <input type="text" name="check_end" id="check_end" required lay-verify="required" placeholder="请输入" lay-reqText="考核结束不能为空" autocomplete="off" class="layui-input">
                    </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:100px;">调取资料时间：</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                <div type="text" id="demo3"  class="xm-select-demo"></div>
                </div>
            </div>
            <div class="layui-row" style="display:none;">
                <button type="submit" class="layui-btn layui-btn-normal"  lay-submit lay-filter="formDemo" id="add-fuyong">提交</button>
            </div>
        </form>
   </div>

   <div id="view-caozuo" style="display:none;padding:10px 40px;">
        <input type="hidden"  name="check_id" value="" >
        <table class="layui-hide" id="ID-table-demo-data1"></table>
        <table class="layui-hide" id="ID-table-demo-data2"></table>

        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px;">添加审核组长</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div id="leader" class="xm-select-demo" style="width: 250px;"></div>
                    </div>
                    <div class="layui-form-mid" style="padding: 0!important;left: 70px;"> 
                        <button type="button" class="layui-btn layui-btn-normal add-qd1" >确定</button>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 100px;">添加审核成员</label>
                    <div class="layui-input-inline layui-input-wrap">
                        <div id="members" class="xm-select-demo" style="width: 250px;"></div>
                    </div>
                    <div class="layui-form-mid" style="padding: 0!important;left: 70px;"> 
                        <button type="button" class="layui-btn layui-btn-normal add-qd2" >确定</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
@endsection
